<?php

namespace Admin\Controller;

class ServiceController extends AdminbaseController {

    protected $serviceModel;

    function _initialize() {
        parent::_initialize();
        $this->serviceModel = D("service");
    }

    function index() {

        $regionId = $_REQUEST['region_id'];

        if(!isNullOrEmpty($regionId)) {
            $map['r.region_id'] = $regionId;
            $this->assign('region_id', $regionId);
            $parameter['region_id'] = $regionId;
        }

        $count = $this->serviceModel->where()->count();
        $page = $this->page($count, 20);
        $list = $this->serviceModel->alias("s")
            ->join("left join __REGION__ r on r.region_id = s.region_id")
            ->field("s.*,r.region_name")
            ->limit($page->firstRow . ',' . $page->listRows)->select();

        $this->assign('status', C('SERVICE_STATUS'));
        $this->assign("list", $list);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

    function add() {
        $this->display();
    }

    /**
     *  添加规则
     */
    function add_post() {
        if (IS_POST) {

            $data['mobile'] = I('post.mobile');
            $data['password'] = md5(I('post.password'));
            $data['service_name'] = I('post.serviceName');
            $data['create_time'] = getCurrentDate();
            $data['status'] = C('SERVICE_STATUS.LEISURE');
            $data["region_id"] = I("post.regionId");
            $data["token"] = createUuid();

            $this->serviceModel->add($data);

            $this->success("添加成功！", U('service/index'));
        }
    }

    /**
     *  编辑规则基本信息
     */
    function edit() {
        $id = I('get.id');
        $service = $this->serviceModel->where(array('service_id' => $id))->find();
        $this->assign('service', $service);
        $this->display();
    }

    /**
     *  编辑规则基本信息
     */
    function edit_post() {
        if (IS_POST) {
            $id = I('post.id');

            $data = $this->serviceModel->where(array('service_id' => $id))->find();
            
            $data['service_name'] = I('post.serviceName');
            $data["region_id"] = I("post.regionId");
            $this->serviceModel->where(array('service_id' => $id))->save($data);

            $this->success("更新成功！", U('service/index'));
        }
    }

    /**
     *  删除
     */
    function delete() {
        $id = intval(I('get.id'));
        $this->serviceModel->delete($id);
        $this->success("删除成功！", U('service/index'));
    }

}
